﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Wicresoft.Logger;

namespace Wicresoft.Test
{
    
    
    /// <summary>
    ///This is a test class for LogTest and is intended
    ///to contain all LogTest Unit Tests
    ///</summary>
    [TestClass()]
    public class LogTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Debug
        ///</summary>
        [TestMethod()]
        public void DebugTest()
        {
            string task = "Test";
            string message = "Log.Debug test " + new Random().Next();
            Log.Debug(task, message);

            using (var dc = new GenericContext())
            {
                Assert.IsNotNull(dc.Logs.FirstOrDefault(p => p.Verb == "Debug" && p.TaskName == task && p.Message == message),
                    "没有找到指定记录");
            }
        }

        /// <summary>
        ///A test for Error
        ///</summary>
        [TestMethod()]
        public void ErrorTest()
        {
            string task = "Test";
            string message = "Log.Error test " + new Random().Next();
            Log.Error(task, message);

            using (var dc = new GenericContext())
            {
                Assert.IsNotNull(dc.Logs.FirstOrDefault(p => p.Verb == "Error" && p.TaskName == task && p.Message == message),
                    "没有找到指定记录");
            }
        }

        /// <summary>
        ///A test for Exception
        ///</summary>
        [TestMethod()]
        public void ExceptionTest()
        {
            string task = "Test";
            Exception e = new ArgumentException("a test exception " + new Random().Next());
            Log.Exception(task, e);

            using (var dc = new GenericContext())
            {
                Assert.IsNotNull(dc.Logs.FirstOrDefault(p => p.Verb == "Exception" && p.TaskName == task && p.Message == e.Message),
                    "没有找到指定记录");
            }
        }

        /// <summary>
        ///A test for Info
        ///</summary>
        [TestMethod()]
        public void InfoTest()
        {
            string task = "Test";
            string message = "Log.Info test " + new Random().Next();
            Log.Info(task, message);

            using (var dc = new GenericContext())
            {
                Assert.IsNotNull(dc.Logs.FirstOrDefault(p => p.Verb == "Info" && p.TaskName == task && p.Message == message), 
                    "没有找到指定记录");
            }
        }
    }
}
